package org.itboys.admin.service.lasted.process;


import org.itboys.admin.entity.lasted.SystemUser;
import org.itboys.admin.entity.lasted.process.SystemProcessTemplateNode;
import org.itboys.admin.service.lasted.SystemUserService;
import org.itboys.commons.utils.tools.ListUtil;
import org.itboys.commons.utils.tools.LongUtil;
import org.itboys.mongodb.core.MongoDataSource;
import org.itboys.mongodb.service.BaseMongoService;
import org.itboys.param.PageResult;
import org.itboys.param.ParamUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * 作者：jiangxiong
 * 日期：2017年2月27日
 * 联系方式 ：empty90911@126.com
 * 描述：系统_流程(审核)_模板__节点_service
 */
@Service
public class SystemProcessTemplateNodeService extends BaseMongoService<SystemProcessTemplateNode> {

    @Autowired
    private SystemUserService userService;


    @Resource(name = "adminDS")
    private MongoDataSource ds;

    @Override
    protected MongoDataSource getMongoDataSource() {
        return ds;
    }

    @Override
    protected Class<SystemProcessTemplateNode> getEntityClass() {
        return SystemProcessTemplateNode.class;
    }


    /**
     * 修改或新增
     * @param templateNode
     * @return
     */
    public Integer  saveTemplateNode(SystemProcessTemplateNode templateNode){

        Map<String,Object> param = new HashMap<String,Object>();
        //新增
        if(templateNode.getId()==0l){
            //校验用户是否已经存在
            param.clear();
            param.put("isDeleted",0);
            param.put("templateId",templateNode.getTemplateId());
            param.put("userId",templateNode.getUserId());
            // param.put("templateId",templateNode.getTemplateId());
            if(ListUtil.isNotNull(super.list(param))){
                return -2;
            }
            //校验需要序号是否唯一
            param.clear();
            param.put("isDeleted",0);
            param.put("templateId",templateNode.getTemplateId());
            param.put("sequence",templateNode.getSequence());
            if(ListUtil.isNotNull(super.list(param))){
                return -3;
            }
            super.save(templateNode);
        //修改
        }else{
            param.clear();
            param.put("isDeleted",0);
            param.put("templateId",templateNode.getTemplateId());
            param.put("userId",templateNode.getUserId());
            param.put("id !=",templateNode.getId());
           // logger.info("-----------ID"+super.list(param).get(0).getUserId());
            // param.put("templateId",templateNode.getTemplateId());
            if(ListUtil.isNotNull(super.list(param))){
                return -2;
            }
            //校验需要序号是否唯一
            param.clear();
            param.put("isDeleted",0);
            param.put("templateId",templateNode.getTemplateId());
            param.put("sequence",templateNode.getSequence());

            param.put("id !=",templateNode.getId());
            if(ListUtil.isNotNull(super.list(param))){
                return -3;
            }
            super.updateExceptEmpty(templateNode.getId(),templateNode);

        }


        return 0;
    }

    /**
     * 分页获取
     * @param request
     * @return
     */
    public PageResult<SystemProcessTemplateNode> selectNode(HttpServletRequest request, Long templateId){
        Map<String,Object> param =new HashMap<String, Object>();
        param.put("isDeleted", 0);
        if(LongUtil.isNotZreo(templateId)){
            param.put("templateId",templateId);
        }
        Map<String,String> containsparam = ParamUtil.packageMongoVagueParam(request,"");
        PageResult<SystemProcessTemplateNode> result=super.containpageQuery(request,containsparam,param);
        //匹配用户名称
        List<Long> userIds = new ArrayList<Long>();
        for(SystemProcessTemplateNode node:result.getData()){
            userIds.add(node.getUserId());
        }
        if(ListUtil.isNotNull(userIds)){

            param.clear();
            param.put("isDeleted",0);
            param.put("id in",userIds);
            List<SystemUser> users = userService.list(param);
            if(ListUtil.isNotNull(users)){
                for(SystemProcessTemplateNode node:result.getData()){
                    for(SystemUser user:users){
                        if(LongUtil.isNotZreo(node.getUserId())&&
                                node.getUserId().longValue()==user.getId()){
                                node.setUserName(user.getName());
                                break;
                        }
                    }
                }
            }

        }
        return result;
    }
    /**
     * 单个获取
     * @param id
     * @return
     */
    public SystemProcessTemplateNode findOne(Long id){
        SystemProcessTemplateNode node = super.getById(id);
        if(LongUtil.isNotZreo(node.getUserId())){
            SystemUser user = userService.getById(node.getUserId());
            node.setUserName(user.getName());
        }
       return node;
    }






}
